#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os
import re

import logging
logging.getLogger().setLevel(logging.INFO)
#logging.error("++++++++++++ 1 cwd = %s, path = %s" % (os.getcwd(), os.environ["Path"] ))

ProjectPath = os.path.dirname(os.getcwd())
sys.path.insert(0, ProjectPath)
sys.path.insert(0, os.path.join(ProjectPath, 'common'))
sys.path.insert(0, os.path.join(ProjectPath, 'database'))

#logging.error("++++++++++++ 2 cwd = %s, path = %s" % (os.getcwd(), os.environ["Path"] ))

from sgmllib import SGMLParser
import wsgiref.handlers

from google.appengine.api import urlfetch
from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template


import commonfunction
import rss.rssdatabase as rssdatabase

from database import function_cloud


class CTestAddCloudPage(commonfunction.CBasePage):
    def get(self):
        TemplateValues = self.GetBaseDefaultTemplateValue()
        TimeDelta = self.GetTheSessionTimeDelta()
        TemplateFilePath = self.GetRightLanguageHtmlFilePath('test_add_cloud')

#    self.response.out.write("TemplateValues : %s" % TemplateValues)
        self.response.out.write(template.render(TemplateFilePath, TemplateValues))
        
    def post(self):
        SourceURL = self.request.get('url', '').strip()
        Title = self.request.get('title', '').strip()
        TagNameString = self.request.get('tag', '').strip()
        Summary = self.request.get('summary', '').strip()
        CommentOn = self.request.get('commenton', '').rstrip()
        IsCommend = int(self.request.get('iscommend', '').strip())

        TagNameString = TagNameString.replace(u"；", u";")
        TagNameString = TagNameString.replace(u"，", u";")
        TagNameString = TagNameString.replace(u",", u";")
        TagNameString = TagNameString.replace(u"　", u";")
        TagNameString = TagNameString.replace(u" ", u";")

        function_cloud.AddOneMyCloud(SourceURL, Title, TagNameString, Summary, CommentOn, IsCommend)
        
        self.redirect('/')

def main():
    application = webapp.WSGIApplication([
                                    ('/test_add_cloud', CTestAddCloudPage),
                                     ],
                                    debug=True)
    
    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()


